package com.cskaoyan.service.mall;

import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.mall.region.Region;
import com.cskaoyan.bean.mall.region.RegionData;
import com.cskaoyan.bean.mall.region.RegionExample;
import com.cskaoyan.mapper.mall.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 行政区域
 * @author: enhui
 **/
@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    RegionMapper regionMapper;

    @Override
    public PageDataVo regionList() {
        RegionExample provinceExample = new RegionExample();
        provinceExample.createCriteria().andTypeEqualTo((byte) 1);
        List<Region> provinces = regionMapper.selectByExample(provinceExample);
        RegionExample cityExample = new RegionExample();
        cityExample.createCriteria().andTypeEqualTo((byte) 2);
        List<Region> cites = regionMapper.selectByExample(cityExample);
        RegionExample areaExample = new RegionExample();
        areaExample.createCriteria().andTypeEqualTo((byte) 3);
        List<Region> areas = regionMapper.selectByExample(areaExample);

        List<RegionData> regionDataList = new ArrayList<>();
        for (Region province : provinces) {
            RegionData regionData = new RegionData();
            regionData.setId(province.getId());
            regionData.setName(province.getName());
            regionData.setType(province.getType());
            regionData.setCode(province.getCode());
            ArrayList<RegionData.CityDOT> cityDTOList = new ArrayList<>();
            for (Region city : cites) {
                if (city.getPid().equals(province.getId())) {
                    RegionData.CityDOT cityDTO = new RegionData.CityDOT();
                    cityDTO.setId(city.getId());
                    cityDTO.setName(city.getName());
                    cityDTO.setType(city.getType());
                    cityDTO.setCode(city.getCode());
                    ArrayList<RegionData.CityDOT.AreaDOT> areaDTOList = new ArrayList<>();
                    for (Region area : areas) {
                        if (area.getPid().equals(city.getId())) {
                            RegionData.CityDOT.AreaDOT areaDTO = new RegionData.CityDOT.AreaDOT();
                            areaDTO.setId(area.getId());
                            areaDTO.setName(area.getName());
                            areaDTO.setType(area.getType());
                            areaDTO.setCode(area.getCode());
                            areaDTOList.add(areaDTO);
                        }
                    }
                    cityDTO.setChildren(areaDTOList);
                    cityDTOList.add(cityDTO);
                }
            }
            regionData.setChildren(cityDTOList);
            regionDataList.add(regionData);
        }
        PageDataVo basePageData = PageDataVo.ok(regionDataList);
        return basePageData;
    }
}
